第一步:
在app>res>drawable>放入要顯示的圖片檔案
第二步:
在app>res>layout> 建立一個activity_welcome.xml並且加入以下code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="#ffffff">
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:contentDescription="@string/app_name"
android:src="@drawable/logo"
android:layout_gravity="center"/>
</LinearLayout>
這裡需要注意:
第三步:
在app>java>專案名稱>建立一個WelcomeActivity.java 並且加入以下code
import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
public class WelcomeActivity extends Activity {
// Splash screen timer
private static int SPLASH_TIME_OUT = 2000; //開啟畫面時間(2秒)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
new Handler().postDelayed(new Runnable() {
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
@Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent intent = new Intent(WelcomeActivity.this, MainActivity.class); //MainActivity為主要檔案名稱
WelcomeActivity.this.startActivity(intent);
// close this activity
WelcomeActivity.this.finish();
}
}, SPLASH_TIME_OUT);
}
}
第四步:修改mainActivity.java
1.把AppCompatActivity改成Activity,否則WelcomeActivity執行完後無法跳轉
public class MainActivity extends Activity {
第五步:修改AndroidManifest.xml
1.把第一個activity修改為WelcomeActivity
<activity android:name=".Activity" >
改為
<activity android:name=".WelcomeActivity" >
2.然後再新增第二個Activity,即為原本的MainActivity
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
</activity>